﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace ListaTelefonicaMEM.Classes
{
    public class Bin
    {
        BinaryFormatter bin = new BinaryFormatter();

        FileStream fsCon = new FileStream(Config.CaminhoApp + "contatos.bin", FileMode.OpenOrCreate);
        FileStream fsTel = new FileStream(Config.CaminhoApp + "telefones.bin", FileMode.OpenOrCreate);


        public void Serializar()
        {
            bin.Serialize(fsCon, lt.Contatos);
            fsCon.Close();

            bin.Serialize(fsTel, lt.Telefones);
            fsTel.Close();
        }

        public void DeSerializar()
        {
            lt.Contatos.Clear();
            lt.Contatos = (List<Contato>)bin.Deserialize(fsCon);
            fsCon.Close();

            lt.Telefones.Clear();
            lt.Telefones = (List<Telefone>)bin.Deserialize(fsTel);
            fsTel.Close();
        }
    }
}
